package com.neu.flashmall.product.controller;


import com.github.pagehelper.PageInfo;
import com.neu.common.utils.Result;
import com.neu.flashmall.product.entity.PmsSkuInfo;
import com.neu.flashmall.product.service.PmsSkuInfoService;
import com.neu.flashmall.product.vo.GoodVo;
import com.neu.flashmall.product.vo.SkuGoodVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/pms_sku")
@Api(value = "商品sku管理接口")
public class PmsSkuInfoController {

    @Resource
    private PmsSkuInfoService pmsSkuInfoService;

    @GetMapping("/getPaged")
    @ApiOperation(value = "获取所有skuInfo的pageInfo")
    public Result getPaged(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "3") int pageSize,
            @Param("spuId") Long spuId){
        return Result.ok().put(pmsSkuInfoService.getPaged(pageNum,pageSize,spuId));
    }

    @GetMapping("selectBySkuName")
    @ApiOperation(value = "获取通过skuName模糊查询的所有skuInfo的pageInfo")
    public Result searchBbySkuName(@Param("skuName")String skuName,
                                   @Param("pageNum") Integer pageNum,
                                   @Param("pageSize") Integer pageSize,
                                   @Param("spuId") Long spuId ){
        PageInfo<PmsSkuInfo> skuList = pmsSkuInfoService.selectBySkuName(skuName,pageNum,pageSize,spuId);
        return Result.ok().put(skuList);
    }

    @DeleteMapping("/check/deleteSku")
    @ApiOperation(value = "删除skuId对应的skuInfo")
    public Result deleteSku(Long skuId){
        String mes = pmsSkuInfoService.delete(skuId);
        if(mes.equals("删除成功")){
            return Result.ok().setMessage(mes);
        }
        return Result.error().setMessage(mes);
    }

    @PostMapping("/check/insert")
    @ApiOperation(value = "添加skuInfo")
    public Result insertSku(@RequestBody PmsSkuInfo pmsSkuInfo){
        String mes = pmsSkuInfoService.insert(pmsSkuInfo);
        if(mes.equals("添加成功")){
            return Result.ok().setMessage(mes);
        }
        return Result.error().setMessage(mes);

    }

    @PostMapping("/check/update")
    @ApiOperation(value = "更新skuInfo")
    public Result updateSkuById(@RequestBody PmsSkuInfo pmsSkuInfo) {
        String mes = pmsSkuInfoService.update(pmsSkuInfo);
        if(mes.equals("修改成功")){
            return Result.ok().setMessage(mes);
        }
        return Result.error().setMessage(mes);
    }

    @GetMapping("/getSkuGoodList")
    public Result getSkuGoodList(Long id){
        List<SkuGoodVo> all = this.pmsSkuInfoService.getAllSkuGood(id);
        return Result.ok().put(all);
    }

    @GetMapping("/getGoodVo")
    public Result getGoodVo(Long id){
        GoodVo result = pmsSkuInfoService.getGoodVo(id);
        return Result.ok().put(result);
    }
}
